import util from "../../common/js/util.js";
var app = getApp();
let baseObj = {
  index: 0,
  title: "",
  CC_LISTS: [],
  data1: [],
  data2: []
}
let baseTitleObj={title1:"",title2:""}
Component({
  mixins: [],
  data: {
    staffNum: 0,
    indexArry: [],//带大写字母分组的 人员 有排序
    staff: [],
    staffArry: null,
    letterMap: new Map(),
    action: "",
    alphabet: [],
    toView: "",
    show: false,
    detailTypeName:"",
    type: 0,// 1审批人 2 抄送人
    subObj: util.copy(baseObj),//提交数据
    index: 0,
    approvalApi:{},
    approvalRuleList: [],//快捷审批人员
    titleObj:util.copy(baseTitleObj)
  },
  props: {
    multiple: true,
    checkStaffList: "",//英文逗号隔开的字符串
    disStaffList: "",
    onPass: function onPass(e) { }
  },
  didMount() {
    this.getApprovalRuleList()
  },
  didUpdate() {
    // this.setData({
    //   multiple: this.props.multiple
    // })
  },
  didUnmount() { },
  methods: {
    getApprovalRuleList() {
      let api = "/YhERP/ApprovalRule/json/list"
      app.$post(api, {}).then(res => {
        if (res.is == 1) {
          this.setData({
            approvalRuleList: res.data.dataList
          })
        } else {
          util.message(res.msg)
        }
      })
    },
    
    openFn(e) {
      //  console.log(this.props.checkStaffList);
      let detailTypeName="";
      if(e&&e.detailTypeName){
        detailTypeName=e.detailTypeName;
      }
      this.setData({
        approvalApi:e
      })
      console.log("---approvalApi.view_data",e.view_data);
      this.setData({
        show: true,
        detailTypeName
      //  subObj: util.copy(baseObj)
      })
      this.initTitleObj()
      if((this.data.detailTypeName=="compoundG2E"||this.data.detailTypeName=="compoundE2G")
      &&util.isEmpty(this.data.subObj.data1)){
        this.initFuHePeopleList();
      }else{
        this.initFuHePeopleList();
      }
    },
    initTitleObj(){
      let titleObj=util.copy(baseTitleObj);
      if(this.data.detailTypeName=="compoundG2E"){
        titleObj.title1="依次审批";
        titleObj.title2="会同审批"
      }
      if(this.data.detailTypeName=="compoundE2G"){
        titleObj.title1="会同审批";
        titleObj.title2="依次审批"
      }
      this.setData({
        titleObj
      })
    },
    //加载 复合审批人员
    initFuHePeopleList(){
      let data=this.data.approvalApi.view_data;
      let APPROVAL_LIST=data.APPROVAL_LIST;
      let APPROVAL_SHOW=data.APPROVAL_SHOW;
      let CC_LIST=data.CC_LIST;
      
      
      if(util.isNotEmpty(APPROVAL_LIST)){
        let list=this.idsParseToApprovalRuleList(APPROVAL_LIST);
        let detailTypeName=this.passDetailTypeName(1,this.data.detailTypeName);
        list.forEach(element => {
          element.no_delete=true;//不可删除 
          element.detailTypeName=detailTypeName;
        });
        this.setData({
          "subObj.data1":list
        })
      }
      if(util.isNotEmpty(APPROVAL_SHOW)){
        let list=this.idsParseToApprovalRuleList(APPROVAL_SHOW);
        let detailTypeName=this.passDetailTypeName(2,this.data.detailTypeName);
        list.forEach(element => {
          element.no_delete=true;//不可删除 
          element.detailTypeName=detailTypeName;

        });
        this.setData({
          "subObj.data2":list
        })
      }
      if(util.isNotEmpty(CC_LIST)){
        let list=this.idsParseToApprovalRuleList(CC_LIST);
        list.forEach(element => {
          element.no_delete=true;//不可删除 
        });
        this.setData({
          "subObj.CC_LISTS":list
        })
      }
    },
    onReset() {
      this.setData({
        subObj: util.copy(baseObj)
      })
    },
    //打开一个默认没有勾选的联系人
    openEmpty() {
      this.setData({
        show: true
      })
    },
    getItemTypeName(detailTypeName){
      return this.passDetailTypeName(this.data.type,detailTypeName)
    },
    passDetailTypeName(type,typeName){
      let detailTypeName="";
      let approvalType=typeName;
      
      if(type==1){
        switch (approvalType) {
          case "examination":
            detailTypeName="examination";
            break;
          case "gradually":
            // 依次审批
            detailTypeName="gradually";

            break;
          case "compoundG2E":
           // text = "依次转会同审批";
           detailTypeName="gradually";
            break;
          case "compoundE2G":
          //  text = "会同转依次审批";
          detailTypeName="examination";
            break;
        }
        
      }else if(type==2){
        switch (approvalType) {
          // case "examination":
          //   detailTypeName="examination";
          //   break;
          // case "gradually":
          //   // 依次审批
          //   detailTypeName="gradually";
          //   break;
          case "compoundG2E":
           // text = "依次转会同审批";
           detailTypeName="examination";
            break;
          case "compoundE2G":
          //  text = "会同转依次审批";
          detailTypeName="gradually";
            break;
        }
      }
      return detailTypeName;
    },
    onIndexPass(list) {
      //console.log(list);
      let RECEIVERS = [];
      for (let index = 0; index < list.length; index++) {
        let user_en_name = list[index];
        let user_cn_name = app.prototype.$USERNAME_MAP.get(user_en_name);
        if (util.isNotEmpty(user_cn_name)) {
          let detailTypeName=this.getItemTypeName(this.data.detailTypeName);
         
          let json = {
            UserName: user_en_name,
            name: user_cn_name,
            detailTypeName,
            UserCnName: user_cn_name
          }
          RECEIVERS.push(json);
        };
      }
      if (this.data.type == 1) {
        let arr=this.data.subObj.data1;
        
        if(this.data.approvalApi.view_data.APPROVAL_LAST=="RearFixation"){
          //后面固定
          arr.unshift(...RECEIVERS)
        }else {
          //前面固定
          arr=arr.concat(RECEIVERS);
        }
        this.setData({
          "subObj.data1": arr
        })
      } else if (this.data.type == 2) {
        let arr2=this.data.subObj.data2;
      //  debugger
        if(this.data.approvalApi.view_data.NOTE=="RearFixation"){
          //后面固定
          arr2.unshift(...RECEIVERS)
        }else{
          //前面固定
          arr2=arr2.concat(RECEIVERS);
        }
        this.setData({
          "subObj.data2": arr2
        })
      }
      else if (this.data.type == 3) {
        let arr3=this.data.subObj.CC_LISTS;
        if(this.data.approvalApi.view_data.PROCESSED=="RearFixation"){
          //后面固定
          arr3.unshift(...RECEIVERS)
        }else if(this.data.approvalApi.view_data.PROCESSED=="FrontFixation"){
          //前面固定
          arr3=arr3.concat(RECEIVERS);
        }else{
          //前面固定
          arr3=arr3.concat(RECEIVERS);
        }
        this.setData({
          "subObj.CC_LISTS": arr3
        })
      }
      
    },
    deleteItem(e) {
      let type = e.currentTarget.dataset.type;
      let index = e.currentTarget.dataset.index;
      let that = this
      dd.confirm({
        title: '温馨提示',
        content: '是否确认删除该审批人?',
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        success: (result) => {
          if (result.confirm) {
            if (type == 1) {
              let list = that.data.subObj.data1;
              list.splice(index, 1);
              that.setData({
                'subObj.data1': list
              })
            } else if (type == 2) {
              let list = that.data.subObj.data2;
              list.splice(index, 1);
              that.setData({
                'subObj.data2': list
              })
            }else if (type == 3) {

              let list = that.data.subObj.CC_LISTS;
              list.splice(index, 1);
              that.setData({
                'subObj.CC_LISTS': list
              })
            }
          }
        },
      });
    },
    idsParseToApprovalRuleList(str = "") {
      let list = str.split(",");
      let RECEIVERS = [];
      for (let index = 0; index < list.length; index++) {
        let user_en_name = list[index];
        let user_cn_name = app.prototype.$USERNAME_MAP.get(user_en_name);
        if (util.isNotEmpty(user_cn_name)) {
          let json = {
            UserName: user_en_name,
            UserCnName: user_cn_name,
            name: user_cn_name,
            detailTypeName:this.data.detailTypeName,
            no_delete:false
          }
          RECEIVERS.push(json);
        };
      }
      return RECEIVERS;
    },
    bindPickerChange(e) {
      //console.log('picker ', e);
      let index = e.detail.value;
      let data1 = [], CC_LISTS = [], title = "";
      let node = this.data.approvalRuleList[index];
      title = node.TITLE;

      
      if(node.TYPE_NAME=='compoundG2E'||node.TYPE_NAME=='compoundE2G'){
        if(util.isNotEmpty(node.APPROVAL_LIST)){
          let appovalJson= JSON.parse(node.APPROVAL_LIST);
          
          let APPROVAL_LIST=appovalJson.APPROVAL_LIST;
          let APPROVAL_SHOW=appovalJson.APPROVAL_SHOW;
          if(util.isNotEmpty(APPROVAL_LIST)){
            let list=this.idsParseToApprovalRuleList(APPROVAL_LIST);
            let detailTypeName=this.passDetailTypeName(1,node.TYPE_NAME);
            list.forEach(element => {
             // element.no_delete=true;//不可删除 
              element.detailTypeName=detailTypeName;
            });
            this.setData({
              "subObj.data1":list
            })
            
          }
          
          if(util.isNotEmpty(APPROVAL_SHOW)){
            let list2=this.idsParseToApprovalRuleList(APPROVAL_SHOW);
            let detailTypeName2=this.passDetailTypeName(2,node.TYPE_NAME);
            list2.forEach(element => {
            //  element.no_delete=true;//不可删除 
              element.detailTypeName=detailTypeName2;
            });
            this.setData({
              "subObj.data2":list2
            })
          }
        }
      }else{

        
        let list3=this.idsParseToApprovalRuleList(node.APPROVAL_LIST);
       
        list3.forEach(element => {
         // element.no_delete=true;//不可删除 
          element.detailTypeName=node.TYPE_NAME;
        });
        this.setData({
          "subObj.data1":list3
        })
      }
    
      CC_LISTS = this.idsParseToApprovalRuleList(node.CC_LIST);
      // let subObj = {
      //   index, CC_LISTS, title
      // }
      this.setData({ 
        "subObj.index":index,
        "subObj.CC_LISTS":CC_LISTS,
        "subObj.title":title,
       });
      // console.log(this.data.subObj);
    },
    onPass() {
      
      this.props.onPass(this.data.subObj);
      this.onCloseFn();
    },

    onCloseFn() {
      this.setData({
        show: false
      });
      // console.log(this.data.show);

     // this.onReset();
    },
    pass() {
      if (this.data.subObj.data1.length == 0) {
        util.message("请选择审批人员!")
        return false;
      }
      // console.log(this.data.staff);
      this.onPass();
    },
    openIndexList(e) {
      let type = e.currentTarget.dataset.type;
      this.setData({
        type
      })
      let disStaffList = "";
      let all_user_name_list=[];
      if (type == 1||type == 2) {
        if(this.data.subObj.data1){
          let list1 = this.data.subObj.data1;
          list1.forEach(element => {
            all_user_name_list.push(element.UserName);
          });
        }
        
        if(util.isNotEmpty(this.data.subObj.data2)){
          let list2 = this.data.subObj.data2;
          list2.forEach(element => {
            all_user_name_list.push(element.UserName);
          });
        }
        
       // disStaffList = userNames.join(",");
      } else if (type == 3) {
        let list = this.data.subObj.CC_LISTS;
        let userNames = [];
        list.forEach(element => {
          userNames.push(element.UserName);
          all_user_name_list.push(element.UserName);
        });
        
      }
      disStaffList = all_user_name_list.join(",");
      this.index_list.openDisFn(disStaffList);
    },
    index_list(ref) {
      this.index_list = ref;
    }
  }

});
